const express = require('express')

const app = express()

const cors = require('cors')

const joi = require('@hapi/joi')
app.use(express.static('html'))
app.use(cors())

app.use(express.urlencoded({ extended: false }))

app.use((req, res, next) => {

    res.cc = function (err, status = 1) {
        res.send({ status, message: err instanceof Error ? err.message : err })
    }
    next()
})

const userRouter = require('./router/user')
app.use('/api', userRouter)

// 定义错误级别的中间件
app.use((err, req, res, next) => {
    if (err instanceof joi.ValidationError) res.cc(err)
    res.cc(err)
})
app.listen(3007, () => {
    console.log('server running at http://127.0.0.1')
})